$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'slideshow/slideshow/list',
        datatype: "json",
        colModel: [
            { label: '轮播图ID', name: 'slideshowid', index: 'SlideshowID', align:"center",width: 50, key: true },
            { label: '轮播图路径', name: 'slideshowurl', index: 'SlideshowURL', align:"center",width: 100 , formatter: function(value, options, row){
                    return "<img @click='imgclick' style='width: 100px;height: 50px' src='"+value+"'>";
                }},
            { label: '标题', name: 'title', index: 'Title', align:"center",width: 80 },
            { label: '文字内容', name: 'content', index: 'Content', align:"center",width: 80 },
            { label: '序号', name: 'slideshoworder', index: 'SlideShowOrder',align:"center", width: 30 },
            { label: '创建时间', name: 'createdate', index: 'CreateDate',align:"center", width: 90 },
            { label: '更新时间', name: 'updatedate', index: 'UpdateDate', align:"center",width: 90 },
            { label: '软删除', name: 'isdel', index: 'IsDel',align:"center", width: 50 , formatter: function(value, options, row){
                    return value === 0 ?
                        '<span class="label label-success">正常</span>' :
                        '<span class="label label-danger">已删除</span>';
                }}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});


var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        slideshow:{isdel:0,slideshoworder:null},
        uploadBase64:null
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.slideshow = {uploadBase64:null,slideshowurl:null,isdel:0}
        },
        update: function (event) {
            var slideshowid = getSelectedRow();
            if(slideshowid == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(slideshowid)
        },
        saveOrUpdate: function (event) {
            var url = vm.slideshow.slideshowid == null ? "slideshow/slideshow/save" : "slideshow/slideshow/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.slideshow),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var slideshowids = getSelectedRows();
            if(slideshowids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "slideshow/slideshow/delete",
                    contentType: "application/json",
                    data: JSON.stringify(slideshowids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(slideshowid){
            $.get(baseURL + "slideshow/slideshow/info/"+slideshowid, function(r){
                vm.slideshow = r.slideshow;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                url:baseURL + "slideshow/slideshow/list",
                page:page
            }).trigger("reloadGrid");
            vm.slideshow={isdel:0};
            vm.uploadBase64 = null;
        }
    }

});

layui.use('upload', function(){
    var upload = layui.upload;

    //执行实例
    var uploadInst = upload.render({
        auto:false,
        bindAction:start,
        elem: '#upload' //绑定元素
        ,url: '/upload/img?type=2' //上传接口
        ,field: 'img',
        accept:'images/jpg, image/png',
        acceptMime: 'image/jpg, image/png'
        ,done: function(res){
            //设置url
            vm.slideshow.slideshowurl = res.data[0];
            alert("上传成功!");
        }
        ,error: function(){
            alert("上传失败!");
        },choose:function (obj) {
            obj.preview(function(index, file, result){
                //预览
                vm.uploadBase64 = result;
            });
        }
    });
});
